package cc.spider.web.model.http;


import cc.spider.model.ResultInfo;

import java.util.*;

public class Response<T> extends ResultInfo<T> {

    private int statusCode = HttpConstant.StatusCode.CODE_OK;

    private boolean downloadSuccess = true;

    private List<IHeader> headers = new ArrayList<>();

    private Map<String, Cookie> cookies = new HashMap<>();

    private String charset = "";

    public Response(T content) {
        super(content);
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public boolean isDownloadSuccess() {
        return downloadSuccess;
    }

    public void setDownloadSuccess(boolean downloadSuccess) {
        this.downloadSuccess = downloadSuccess;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Map<String, Cookie> getCookies() {
        return cookies;
    }

    public void setCookies(Map<String, Cookie> cookies) {
        this.cookies = cookies;
    }

    public void addCookie(Cookie cookie) {
        this.cookies.put(cookie.getName(), cookie);
    }

    public void addCookie(String name, String val){
        this.cookies.put(name, new Cookie(name, val));
    }

    public List<IHeader> getHeaders() {
        return headers;
    }

    public List<IHeader> getHeaders(String headerName){
        List<IHeader> headers = new ArrayList<>();
        for (IHeader header: this.headers){
            if(header.getName().equals(headerName)){
                headers.add(header);
            }
        }
        return headers;
    }

    public void setHeaders(List<IHeader> headers) {
        this.headers = headers;
    }

    public void addHeader(String name, String val){
        IHeader header = new Header(name, val);
        this.headers.add(header);
    }

    public void addHeader(IHeader header){
        this.headers.add(header);
    }

    public void addHeaders(IHeader[] headers){
        this.headers.addAll(Arrays.asList(headers));
    }

}
